﻿using System;
using System.Data;

namespace DAL
{
    public static class DalSell
    {
        private static readonly clsConnect Cls = new clsConnect();
        #region[Get-Data]
        public static DataTable GetSell(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_Sell_Get");
        }
        public static DataTable GetSellEdit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", id);
            return Cls.GetData("sp_Sell_Get_Edit");
        }
        public static DataTable GetOrderSellDetailExists()
        {
            Cls.CreateNewSqlCommand();
            return Cls.GetData("sp_Sell_Detail_Check_Exists");
        }
        public static DataTable GetOrderSellDetailTemp(int userId)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", userId);
            return Cls.GetData("sp_tb_Sell_Detail_Get_Temp");
        }
        public static DataTable GetOrderSellDetailEditExist(int userId, int idSell)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", userId);
            Cls.AddParameter("@ID_Sell", idSell);
            return Cls.GetData("sp_tb_Sell_Detail_Get_Exist");
        }
        public static DataTable GetOrderSellDetailEdit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", id);
            return Cls.GetData("sp_tb_Sell_Detail_Get_Edit");
        }
        public static DataTable GetOrderSellDetailEditA(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", id);
            return Cls.GetData("sp_tb_Sell_Detail_Get_Edit_A_New");
        }
        public static DataTable GetOrderSellDetailEditB(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", id);
            return Cls.GetData("sp_tb_Sell_Detail_Get_Edit_B_New");
        }
        public static DataTable GetOrderSellDetailSum(int userId, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Users_Id", userId);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Sell_Detail_Sum");
        }
        public static DataTable GetOrderSellDetailSumEdit(int id, int flag)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", id);
            Cls.AddParameter("@Flag", flag);
            return Cls.GetData("sp_Sell_Detail_Sum_Edit");
        }
        public static DataTable GetOrderSell(string keywords)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@KEYWORDS", keywords);
            return Cls.GetData("sp_tb_Order_Sell_Get_Active");
        }
        public static DataTable GetOrderSellEdit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", id);
            return Cls.GetData("sp_tb_Order_Sell_Get_Active_Edit");
        }
        public static DataTable GetOrderSellGridEdit(int id)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", id);
            return Cls.GetData("sp_tb_Order_Sell_Detail_Get");
        }
        public static bool GetOrderSellGridCheck(EtOrderSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_tb_Order_Sell_Detail_Get", "@Msg");
            return true;
        }
        public static DataTable GetOrderSellFillterDate(string dateBegin, string dateEnd)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@DateBegin", dateBegin);
            Cls.AddParameter("@EndBegin", dateEnd);
            return Cls.GetData("sp_Sell_FillterDate");
        }
        #endregion

        #region[Insert-Update-Delete]
        public static bool InsertSell(EtSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Customer_Id", obj.Customer_Id);
            Cls.AddParameter("@Sell_Code", obj.Sell_Code);
            Cls.AddParameter("@Sell_DateBegin", obj.Sell_DateBegin);
            Cls.AddParameter("@Sell_Tax_Code", obj.Sell_Tax_Code);
            Cls.AddParameter("@Sell_User_Id", obj.Sell_User_Id);
            Cls.AddParameter("@IsActive", obj.IsActive);
            Cls.AddParameter("@IsUsed", obj.IsUsed);
            Cls.AddParameter("@Note", obj.Note);
            Cls.AddParameter("@TypeSell", obj.TypeSell);
            return Cls.ExecuteNonQuery("sp_Sell_Insert");
        }
        public static bool InsertSellDetail(EtSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@Materials_Id", obj.Materials_Id);
            Cls.AddParameter("@Sell_Detail_Quanlity", obj.Sell_Detail_Quanlity);
            Cls.AddParameter("@Sell_Detail_Price", obj.Sell_Detail_Price);
            Cls.AddParameter("@Sell_Detail_CK_Percent", obj.Sell_Detail_CK_Percent);
            Cls.AddParameter("@Sell_Detail_CK", obj.Sell_Detail_CK);
            Cls.AddParameter("@Sell_Detail_VAT_Percent", obj.Sell_Detail_VAT_Percent);
            Cls.AddParameter("@Sell_Detail_VAT", obj.Sell_Detail_VAT);
            Cls.AddParameter("@Sell_Detail_Total_Price", obj.Sell_Detail_Total_Price);
            Cls.AddParameter("@IsActive", obj.IsActive);
            Cls.AddParameter("@Sell_Detail_Quality_Remain", obj.Sell_Detail_Quality_Remain);
            Cls.AddParameter("@Flag", obj.Flag);
            Cls.AddParameter("@User_ID", obj.User_ID);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Sell_Detail_Insert", "@Msg");
            return true;
        }
        public static bool UpdateQuanlityOrder(EtSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Materials_ID", obj.Materials_Id);
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@ID_Sell_Detail", obj.ID_Sell_Detail);
            Cls.AddParameter("@Sell_Detail_Quanlity", obj.Sell_Detail_Quanlity);
            Cls.AddParameter("@Sell_Detail_Price", obj.Sell_Detail_Price);
            Cls.AddParameter("@Sell_Detail_CK_Percent", obj.Sell_Detail_CK_Percent);
            Cls.AddParameter("@Sell_Detail_VAT_Percent", obj.Sell_Detail_VAT_Percent);
            Cls.AddParameter("@Sell_Detail_Quality_Remain", obj.Sell_Detail_Quality_Remain);
            Cls.AddParameter("@Flag", obj.Flag);
            Cls.AddParameter("@Users_Id", obj.User_ID);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_tb_Sell_Detail_Update_Quanlity", "@Msg");
            return true;
        }
        public static bool UpdateQuanlityOrderRoot(EtSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Materials_ID", obj.Materials_Id);
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@ID_Sell_Detail", obj.ID_Sell_Detail);
            Cls.AddParameter("@Sell_Detail_Quanlity", obj.Sell_Detail_Quanlity);
            Cls.AddParameter("@Sell_Detail_CK_Percent", obj.Sell_Detail_CK_Percent);
            Cls.AddParameter("@Sell_Detail_VAT_Percent", obj.Sell_Detail_VAT_Percent);
            Cls.AddParameter("@Sell_Detail_Quality_Remain", obj.Sell_Detail_Quality_Remain);
            Cls.AddParameter("@Flag", obj.Flag);
            Cls.AddParameter("@User_ID", obj.User_ID);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_tb_Sell_Detail_Update_Quanlity_Root", "@Msg");
            return true;
        }
        public static bool UpdateOrderSell(EtSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@Sell_Tax_Code", obj.Sell_Tax_Code);
            Cls.AddParameter("@Users_Id", obj.Sell_User_Id);
            Cls.AddParameter("@Note", obj.Note);
            return Cls.ExecuteNonQuery("sp_tb_Sell_Update");
        }
        public static bool DeleteSellDetailMaterial(EtSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell_Detail", obj.ID_Sell_Detail);
            return Cls.ExecuteNonQuery("sp_Sell_Detail_DelMaterial");
        }
        public static bool DeleteSellOrder(EtSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@User_ID", obj.User_ID);
            return Cls.ExecuteNonQuery("sp_Sell_Detail_Del");
        }
        public static bool DeleteSell(EtSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            obj.Msg = Cls.ExecuteNonQueryOutput("sp_Sell_Delete", "@Msg");
            return true;
        }
        //-----Insert-Order-Sell
        public static bool InsertSellWithOrder(EtSell obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@Customer_Id", obj.Customer_Id);
            Cls.AddParameter("@Sell_Code", obj.Sell_Code);
            Cls.AddParameter("@Sell_DateBegin", obj.Sell_DateBegin);
            Cls.AddParameter("@Sell_Tax_Code", obj.Sell_Tax_Code);
            Cls.AddParameter("@Sell_User_Id", obj.Sell_User_Id);
            Cls.AddParameter("@IsActive", obj.IsActive);
            Cls.AddParameter("@IsUsed", obj.IsUsed);
            Cls.AddParameter("@Note", obj.Note);
            Cls.AddParameter("@TypeSell", obj.TypeSell);
            return Cls.ExecuteNonQuery("sp_Sell_Insert_WithOrder");
        }
        public static bool InsertSellDetailWithOrder(EtSellDetail obj)
        {
            Cls.CreateNewSqlCommand();
            Cls.AddParameter("@ID_Sell", obj.ID_Sell);
            Cls.AddParameter("@Order_Sell_Id", obj.Order_Sell_Id);
            Cls.AddParameter("@User_ID", obj.User_ID);
            return Cls.ExecuteNonQuery("sp_Sell_Detail_Insert_WithOrder");
        }
        #endregion
    }

    public class EtSell
    {
        public int ID_Sell { get; set; }
        public int ? Order_Sell_Id { get; set; }
        public int ? Customer_Id { get; set; }
        public string Sell_Code { get; set; }
        public DateTime Sell_DateBegin { get; set; }
        public string Sell_Tax_Code { get; set; }
        public int Sell_User_Id { get; set; }
        public bool IsActive { get; set; }
        public bool IsUsed { get; set; }
        public string Note { get; set; }
        public string TypeSell { get; set; }
        public int Msg { get; set; }
    }
    public class EtSellDetail
    {
        public int ID_Sell_Detail { get; set; }
        public int ? ID_Sell { get; set; }
        public int ? Materials_Id { get; set; }
        public int ? Sell_Detail_Quanlity { get; set; }
        public float? Sell_Detail_Price { get; set; }
        public float ? Sell_Detail_CK_Percent { get; set; }
        public float ? Sell_Detail_CK { get; set; }
        public float ? Sell_Detail_VAT_Percent { get; set; }
        public float ? Sell_Detail_VAT { get; set; }
        public float ? Sell_Detail_Total_Price { get; set; }
        public int? Order_Sell_Id { get; set; }
        public bool IsActive { get; set; }
        public int Sell_Detail_Quality_Remain { get; set; }
        public int Flag { get; set; }
        public int User_ID { get; set; }
        public int Msg { get; set; }
    }
}